from flask import Flask, request, jsonify
import mysql.connector
from flask_cors import CORS

app = Flask(__name__)
CORS(app)
# 配置数据库连接
db_config = {
    'user': 'root',
    'password': '1qaz@WSX#EDC',
    'host': '106.54.211.99',
    'database': 'linux_exam'
}

def get_db_connection():
    connection = mysql.connector.connect(**db_config)
    return connection

@app.route('/add_book', methods=['GET'])
def add_book():
    number = request.args.get('number')
    name = request.args.get('name')
    author = request.args.get('author')

    if not number or not name or not author:
        return jsonify({'message': 'Missing parameters'}), 400

    connection = get_db_connection()
    cursor = connection.cursor()

    try:
        cursor.execute("INSERT INTO t_book (number, name, author) VALUES (%s, %s, %s)", (number, name, author))
        connection.commit()
        return jsonify({'message': 'Book added successfully'}), 200
    except mysql.connector.Error as err:
        return jsonify({'message': str(err)}), 500
    finally:
        cursor.close()
        connection.close()

@app.route('/list_book', methods=['GET'])
def list_books():
    connection = get_db_connection()
    cursor = connection.cursor(dictionary=True)

    try:
        cursor.execute("SELECT * FROM t_book")
        books = cursor.fetchall()
        return jsonify(books), 200
    except mysql.connector.Error as err:
        return jsonify({'message': str(err)}), 500
    finally:
        cursor.close()
        connection.close()

@app.route('/get_book', methods=['GET'])
def get_book():
    number = request.args.get('number')

    if not number:
        return jsonify({'message': 'Missing parameter'}), 400

    connection = get_db_connection()
    cursor = connection.cursor(dictionary=True)

    try:
        cursor.execute("SELECT * FROM t_book WHERE number = %s", (number,))
        book = cursor.fetchone()
        if book:
            return jsonify(book), 200
        else:
            return jsonify({'message': 'Book not found'}), 404
    except mysql.connector.Error as err:
        return jsonify({'message': str(err)}), 500
    finally:
        cursor.close()
        connection.close()

@app.route('/delete_book', methods=['GET'])
def delete_book():
    number = request.args.get('number')

    if not number:
        return jsonify({'message': 'Missing parameter'}), 400

    connection = get_db_connection()
    cursor = connection.cursor()

    try:
        cursor.execute("DELETE FROM t_book WHERE number = %s", (number,))
        connection.commit()
        if cursor.rowcount > 0:
            return jsonify({'message': 'Book deleted successfully'}), 200
        else:
            return jsonify({'message': 'Book not found'}), 404
    except mysql.connector.Error as err:
        return jsonify({'message': str(err)}), 500
    finally:
        cursor.close()
        connection.close()

@app.route('/update_book', methods=['GET'])
def update_book():
    number = request.args.get('number')
    name = request.args.get('name')
    author = request.args.get('author')

    if not number or not name or not author:
        return jsonify({'message': 'Missing parameters'}), 400

    connection = get_db_connection()
    cursor = connection.cursor()

    try:
        cursor.execute("UPDATE t_book SET name = %s, author = %s WHERE number = %s", (name, author, number))
        connection.commit()
        if cursor.rowcount > 0:
            return jsonify({'message': 'Book updated successfully'}), 200
        else:
            return jsonify({'message': 'Book not found'}), 404
    except mysql.connector.Error as err:
        return jsonify({'message': str(err)}), 500
    finally:
        cursor.close()
        connection.close()

if __name__ == '__main__':
    app.run(debug=True)

